<?php
// +----------------------------------------------------------------------
// | ZGUAN [ 致力于帮助更多的中小企业物联网转型 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2017~2024 https://www.zguan.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ZGUAN 并不是自由软件，未经许可不能去掉ZGUAN 相关版权
// +----------------------------------------------------------------------
// | Author: ZGUAN Team <admin@zguan.cn>  0769-82282996  15899929162
// +----------------------------------------------------------------------
namespace app\controller\property\article\faq;

use think\App;
use app\controller\property\Base;
use think\exception\ValidateException;
use app\validate\article\faq\FaqValidate;
use app\common\repositories\article\faq\FaqRepository;
use app\common\repositories\article\faq\FaqCateRepository;


class ArticleFaq extends Base
{
    protected $repository;

    public function __construct(App $app, FaqRepository $repository)
    {
        parent::__construct($app);
        $this->repository = $repository;
        $this->assign([
            'addAuth' => property_auth('propertyArticleFaqAdd'),
            'editAuth' => property_auth('propertyArticleFaqEdit'),
            'delAuth' => property_auth('propertyArticleFaqDel'),
            'detailAuth' => property_auth('propertyArticleFaqDetail'),
            'switchStatusAuth' => property_auth('propertyArticleFaqTop'),
            'placedTopAuth' => property_auth('propertyArticleFaqShow'),
        ]);
    }

    protected function commonParams()
    {
        /**
         * @var FaqCateRepository $faqCateRepository
         */
        $faqCateRepository = app()->make(FaqCateRepository::class);
        $cateData = $faqCateRepository->getCascaderData($this->request->propertyId, ['is_show' => 1]);
        $this->assign([
            'cateData' => $cateData,
        ]);
    }

    public function list()
    {
        if ($this->request->isAjax()) {
            $where = $this->request->param([
                'cate_id' => '',
                'keywords' => ''
            ]);
            [$page, $limit] = $this->getPage();
            $data = $this->repository->getpropertyList($where, $page, $limit, $this->request->propertyId);
            return json()->data(['code' => 0, 'data' => $data['list'], 'count' => $data['count']]);
        }
        $this->commonParams();
        return $this->fetch('article/faq/list');
    }

    /**
     * 添加常见问题
     * @return string|\think\response\Json|\think\response\View
     * @throws \Exception
     *
     * @author Amos
     * 2024-01-30 10:52
     */
    public function add()
    {
        if ($this->request->isPost()) {
            $param = $this->request->param([
                'title' => '',
                'picture' => '',
                'keywords' => '',
                'is_show' => '',
                'is_top' => '',
                'content' => '',
                'cate_id' => '',
                'content_type' => '',
                'network_url' => '',
            ]);
            validate(FaqValidate::class)->scene('add')->check($param);
            try {
                $param['content'] = htmlspecialchars($param['content']);
                $res = $this->repository->addInfo($param, $this->request->propertyId, $this->request->userId);
                if ($res) {
                    return $this->success('添加成功');
                } else {
                    return $this->error('添加失败');
                }
            } catch (\Exception $e) {
                exception_log('添加常见问题失败', $e);
                return $this->error('网络失败');
            }
        } else {
            $this->commonParams();
            return $this->fetch('article/faq/add');
        }
    }

    /**
     * 编辑常见问题
     * @return string|\think\response\Json|\think\response\View
     * @throws \Exception
     *
     * @author Amos
     * 2024-01-30 10:52
     */

    public function edit()
    {
        $id = (int)$this->request->param('id');
        if (!$id) {
            return $this->error('参数错误');
        }
        $info = $this->repository->getDetail($id);
        if (!$info) {
            return $this->error('信息错误');
        }
        if ($this->request->isPost()) {
            $param = $this->request->param([
                'id' => '',
                'title' => '',
                'picture' => '',
                'is_show' => '',
                'is_top' => '',
                'content' => '',
                'cate_id' => '',
                'content_type' => '',
                'network_url' => '',
            ]);
            validate(FaqValidate::class)->scene('edit')->check($param);
            try {
                $param['content'] = htmlspecialchars($param['content']);
                $res = $this->repository->editInfo($info, $param, $this->request->userId);
                if ($res) {
                    return $this->success('修改成功');
                } else {
                    return $this->error('修改失败');
                }
            } catch (\Exception $e) {
                exception_log('编辑常见问题失败', $e);
                return $this->error('网络错误');
            }
        } else {
            $this->commonParams();
            return $this->fetch('article/faq/edit', [
                'info' => $info,
            ]);
        }
    }

    /**
     * 删除常见问题
     * @return string|\think\response\Json|\think\response\View
     *
     * @author Amos
     * 2024-01-30 10:52
     */
    public function del()
    {
        $ids = (array)$this->request->param('ids');
        try {
            $data = $this->repository->batchDelete($ids, $this->request->userId);
            if ($data) {
                return $this->success('删除成功');
            } else {
                return $this->error('删除失败');
            }
        } catch (\Exception $e) {
            exception_log('删除常见问题失败', $e);
            return $this->error('网络失败');
        }
    }

    /**
     * 文章详情
     * @return string|\think\response\Json|\think\response\View
     * @throws \Exception
     *
     * @author Amos
     * 2024-01-30 19:09
     */
    public function detail()
    {
        $id = (int)$this->request->param('id');
        if ($id <= 0) {
            return $this->error('参数错误');
        }
        $info = $this->repository->getDetail($id);
        if ($info['id'] <= 0) {
            return $this->error('信息不存在');
        }
        return $this->fetch('article/faq/detail', [
            'info' => $info,
        ]);
    }

    /**
     * 状态更新
     * @return string|\think\response\Json|\think\response\View
     * @throws \think\db\exception\DbException
     *
     * @author Amos
     * 2024-01-30 19:14
     */
    public function switchStatus()
    {
        $id = $this->request->param('id');
        $status = $this->request->param('status', 2) == 1 ? 1 : 2;
        $res = $this->repository->switchStatus($id, $status);
        if ($res) {
            return $this->success('修改成功');
        } else {
            return $this->error('修改失败');
        }
    }

    /**
     * 置顶更新
     * @return string|\think\response\Json|\think\response\View
     *
     * @author Amos
     * 2024-01-30 19:15
     */
    public function placedTop()
    {
        $id = $this->request->param('id');
        $placedTop = $this->request->param('statusTop', 2) == 1 ? 1 : 2;
        $res = $this->repository->placedTop($id, $placedTop);
        if ($res) {
            return $this->success('修改成功');
        } else {
            return $this->error('修改失败');
        }
    }

}